<?php

namespace backend\models\system;

use common\models\system\DictCateModel;

/**
 * Desc 字典
 */
class DictCate extends DictCateModel {

    /**
     * 获取单条数据
     * @param array $condition 搜索条件 如：$condition = ['and'];$condition[] = ['=','id',1];$condition[] = ['LIKE','name','dkh'];
     * @param string $order_by 排序 
     * @param array $select  排获取字段
     * @return array
     * * */
    public static function getDataOne($condition = [], $order_by = 'id DESC', $select = []) {
        $query = self::find()->select($select);
        $query->orderBy($order_by);
        if ($condition) {
            $query->where($condition);
        }
        return $query->asArray()->one();
    }

    /**
     * 获取多条数据
     * @param array $condition [AND,['=','id',1],['','',''],...] 搜索条件
     * @param int $page 页数 
     * @param int $page_size 每页总数 
     * @param string $order_by 排序 
     * @param array $select  排获取字段
     * @return array
     * * */
    public static function getDataAll($condition = [], $page = 1, $page_size = 20, $order_by = 'id DESC', $select = []) {
        $query = self::find()->select($select);
        list($offset, $limit) = \common\tools\Util::getLimit($page['page'], $page_size);
        if ($condition) {
            $query->where($condition);
        }
        $data['total'] = intval($query->count());
        $query->offset($offset)->limit($limit)->orderBy($order_by);
        $data['list'] = $query->asArray()->all();
        return $data;
    }

    /**
     * 新增/编辑
     * @param array $params
     * * */
    public static function create($params) {
        if (!is_array($params)) {
            return false;
        }
        if (self::findOne(['code' => $params['code']])) {
            return false;
        }
        $model = new self();
        $model->setAttributes($params, false);
        return $model->save();
    }
    
    
    /**
     * 根据条件更新对应的字段
     * @param array $params
     * @param array $condition
     * * */
    public static function updateData($params,$condition) {
        if (empty($condition) || empty($params)) {
            return false;
        }
        if (!is_array($condition) || !is_array($params)) {
            return false;
        }
        if (empty(self::find()->where($condition)->all())) {
            return false;
        }
        return self::updateAll($params,$condition);
    }
    
     /**
     * 根据条件删除
     * @param array $condition
     * * */
    public static function del($condition) {
        if (empty($condition)) {
            return false;
        }
        if (empty(self::find()->where($condition)->all())) {
            return false;
        }
        return self::deleteAll($condition);
    }

    /**
     * 获取多条数据-后续再优化
     * @param array $condition [AND,['=','id',1],['','',''],...] 搜索条件
     * @param int $page 页数 
     * @param int $page_size 每页总数 
     * @param array $tables 关联表-['table1','table2']
     * @param string $order_by 排序 
     * @param array $select  排获取字段
     * @param string $join_type  连表类型- LEFT JOIN ,INNER JOIN RIGHT JOIN
     * @param string $distinct 是否去重
     * @return array
     * * */
    public function getDataAllByJoin($condition = [], $page = 1, $page_size = 10, $tables, $order_by = '', $select = [], $join_type = 'INNER JOIN', $distinct = false) {
        $query = self::find()->select($select);
        $query->joinWith($tables, true, $join_type);
        list($offset, $limit) = \common\tools\Util::getLimit($page['page'], $page_size);
        $query->offset($offset)->limit($limit)->orderBy($order_by);
        if ($distinct == true) {
            $query->distinct();
        }
        if ($condition) {
            $query->where($condition);
        }
        return $query->asArray()->all();
    }

}
